﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;

using RexToy;
using RexToy.Xml;
using RexToy.UI.WinForm.Controls;
using RexToy.UI.WinForm.Pmlx;

using MGAS.Model;
using MGAS.Model.Entity;

namespace MGAS.UI.Controls
{
    public class FloorSwitcher : System.Windows.Forms.UserControl, ITemplateItem
    {
        public int ButtonHeight { get; set; }
        public int ButtonWidth { get; set; }
        public int ItemPerColumn { get; set; }

        public event EventHandler<FloorSwitchEventArgs> FloorSwitched;

        private List<Button> _buttons;

        private void FloorSwitcher_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (FloorSwitched != null)
            {
                FloorSwitchEventArgs args = new FloorSwitchEventArgs();
                args.Location = (Location)btn.Tag;

                FloorSwitched(this, args);
            }
        }

        public void ParseTemplate(PmlxNode template)
        {
            _buttons = new List<Button>();

            var buidlings = AllocMap.Instance().FindAllBuildings();
            var locs = AllocMap.Instance().FindAllLocations();
            foreach (var b in buidlings)
            {
                var g = new AutoAlignGroupBox();
                g.Dock = System.Windows.Forms.DockStyle.Left;
                g.Text = b.Text;

                this.Controls.Add(g);

                var q = from loc in locs
                        where loc.BuildingId == b.Id
                        select loc;

                g.SetItemPerColumn(this.ItemPerColumn);
                g.SetMaxCount(q.Count());
                g.Initialize(this.ButtonWidth);

                foreach (var loc in q)
                {
                    Button btn = new Button();
                    _buttons.Add(btn);
                    btn.Dock = System.Windows.Forms.DockStyle.Top;
                    btn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;

                    g.AppendControl(btn);

                    btn.Tag = loc;
                    btn.Height = this.ButtonHeight;
                    btn.Text = loc.Text;

                    btn.Click += new EventHandler(FloorSwitcher_Click);
                }
            }
        }

        public void SetAlarmStatus(Location loc, bool alarm)
        {
            if (this.IsDisposed || this.Disposing)
                return;

            foreach (var btn in _buttons)
            {
                Location l = (Location)btn.Tag;
                if (l.Id == loc.Id)
                {
                    if (alarm)
                    {
                        btn.BackColor = Color.Orange;
                    }
                    else
                    {
                        btn.BackColor = SystemColors.Control;
                    }
                }
            }
        }
    }
}
